19. Maven Plugins

Maven Plugins

ND079 JPND C3 L2 A16 Maven Plugins

00:00
00:00

Maven Plugins

All Maven goals, even the default behaviors, are performed by plugins. Here are the default bindings for JAR projects. You can see that each phase has a groupId, artifactId, and version preceding an additional string, which is the name of the goal that plugin will execute. For examples, in the <compile> phase, we execute the goal compile of the plugin maven-compiler-plugin. That plugin is in the group org.apache.maven.plugins and the default version is 3.1.

<phases>
  <process-resources> org.apache.maven.plugins:maven-resources-plugin:2.6:resources </process-resources>
  <compile> org.apache.maven.plugins:maven-compiler-plugin:3.1:compile </compile>
  <process-test-resources> org.apache.maven.plugins:maven-resources-plugin:2.6:testResources </process-test-resources>
  <test-compile> org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile </test-compile>
  <test> org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test </test>
  <package> org.apache.maven.plugins:maven-jar-plugin:2.4:jar </package>
  <install> org.apache.maven.plugins:maven-install-plugin:2.4:install </install>
  <deploy> org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy </deploy>
</phases>

You can find a list of a wide variety of plugins used by Maven on the documentation site:
https://maven.apache.org/plugins/index.html

Plugin Management vs. Plugins.

You can customize the versions of these plugins in the <pluginManagement> element of the build. Changes in that area will affect all children projects as well. If you wish to customize a plugin for this project only, place those changes in the <plugins> element instead.

<build>
  <plugins>
    <plugin>
      ... customization for this project only
    </plugin>
  </plugins>
  <pluginManagement>
    <plugins>
      <plugin>
        ... customization for all projects that inherit this as well
      </plugin>
    </plugins>
  </pluginManagement>

Customizing Plugin Execution

ND079 JPND C3 L2 A17 Customizing Plugin Execution V3

00:00
00:00

Binding Goal to Phase

To bind a plugin goal to a phase, we add an <execution> element to the plugin definition. This example binds the do-blockchain goal of the plugin called my-blockchain-plugin to the test phase. Now whenever Maven executes that phase, my goal will be run.

<plugin>
  <groupId>com.udacity.jpnd</groupId>

  <artifactId>my-blockchain-plugin</artifactId>
  <executions>
    <execution>
      <phase>test</phase>
      <goals>
        <goal>do-blockchain</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Configuring Plugins

You can pass additional properties to plugins using the <configuration> element. We can modify the above to pass in the <bitcoins> property to our plugin with the following example:

<plugin>
  <groupId>com.udacity.jpnd</groupId>
  <artifactId>my-blockchain-plugin</artifactId>
  <executions>
    <execution>
      <phase>test</phase>
      <goals>
        <goal>do-blockchain</goal>
      </goals>
      <configuration>
        <bitcoins>all</bitcoins>
      </configuration>
    </execution>
  </executions>
</plugin>